package cfs.controller;

/*
 * 
 To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import cfs.databean.Customer;
import cfs.model.CustomerDAO;
import cfs.model.Model;

/**
 * 
 * @author ting wu
 */
public class ViewAllCustomerAction extends Action {

	private CustomerDAO cusDAO;

	public ViewAllCustomerAction(Model model) {
		cusDAO = model.getCustomerDao();

	}

	@Override
	public String getName() {
		return "viewCustomerList.do";
	}

	@Override
	public String perform(HttpServletRequest request) {
		List<String> errors = new ArrayList<String>();
		request.setAttribute("errors", errors);
		try {
			// Fetching the values from the database
			Customer[] customerList = cusDAO.getAll();
			if (customerList == null || customerList.length == 0) {
				errors.add("No customer found");
			}
			if (errors.size() > 0)
				return "ViewAllCustomerList.jsp";

			request.setAttribute("customerList", customerList);
			return "ViewAllCustomerList.jsp";
		} catch (Exception e) {
			errors.add("error in requesting customer list");
			return "ViewAllCustomerList.jsp";
		}
	}

}
